package com.tj.jinzao.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.text.style.ImageSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.netease.nim.uikit.api.NimUIKit;
import com.netease.nim.uikit.business.session.emoji.MoonUtil;
import com.tj.jinzao.R;
import com.tj.jinzao.common.Constant;
import com.tj.jinzao.model.Article;
import com.tj.jinzao.utils.Utils;
import com.tj.jinzao.widgets.RoundedImageView;

import java.util.List;

public class ArticleDeaAdapter extends Base_Adapter {
    public ArticleDeaAdapter(Context context, List list, boolean isFirst) {
        super(context, list, isFirst);
    }

    @Override
    public View getView(int position, View itemView, ViewGroup parent) {
        ViewHolder viewHolder;
        if(itemView==null){
            viewHolder=new ViewHolder();
            itemView= LayoutInflater.from(context).inflate(R.layout.item_article_list_bot,null);
            viewHolder.name = itemView.findViewById(R.id.tv_name);
            viewHolder.content_lay = itemView.findViewById(R.id.content_lay);
            viewHolder.vip = itemView.findViewById(R.id.tv_vip);
            viewHolder.date = itemView.findViewById(R.id.tv_date);
            viewHolder.content = itemView.findViewById(R.id.tv_content);
            viewHolder.label = itemView.findViewById(R.id.tv_label);
            viewHolder.views = itemView.findViewById(R.id.tv_view);
            viewHolder.praises = itemView.findViewById(R.id.tv_praise);
            viewHolder.comments = itemView.findViewById(R.id.tv_comment);
            viewHolder.head = itemView.findViewById(R.id.iv_head);
            viewHolder.imageLayout = itemView.findViewById(R.id.ll_image_layout);
            viewHolder.image1 = itemView.findViewById(R.id.iv_image_1);
            int itemWidth = (Constant.SCREEN_WIDTH - Utils.dpToPx(74, context.getResources())) / 2;
            ViewGroup.LayoutParams layoutParams = viewHolder.image1.getLayoutParams();
            layoutParams.width = itemWidth;
            layoutParams.height = itemWidth;
            viewHolder.image1.setLayoutParams(layoutParams);
            viewHolder.image2 = itemView.findViewById(R.id.iv_image_2);
            viewHolder.image = itemView.findViewById(R.id.iv_image);
            ViewGroup.LayoutParams layoutParams1 = viewHolder.image2.getLayoutParams();
            layoutParams1.width = itemWidth;
            layoutParams1.height = itemWidth;
            viewHolder.image2.setLayoutParams(layoutParams1);
            ViewGroup.LayoutParams layoutParams2 = viewHolder.image.getLayoutParams();
            layoutParams2.width = Constant.SCREEN_WIDTH - Utils.dpToPx(64, context.getResources());
            layoutParams2.height = itemWidth;
            viewHolder.image.setLayoutParams(layoutParams2);
            viewHolder.play = itemView.findViewById(R.id.iv_play);
            itemView.setTag(viewHolder);
        }else {
            viewHolder= (ViewHolder) itemView.getTag();
        }
        Article article = (Article) getItem(position);
        if(article!=null&&article.getInvid()!=1){
            viewHolder.content_lay.setVisibility(View.VISIBLE);
            String imageUrl = article.getImgurls();
            viewHolder.date.setText(article.getReleaseTime());
            if (TextUtils.isEmpty(imageUrl)) {
                viewHolder.imageLayout.setVisibility(View.GONE);
            } else {
                String images[] = imageUrl.split(",");
                viewHolder.imageLayout.setVisibility(View.VISIBLE);
                if (images.length == 1 || article.isVideo()) {
                    viewHolder.image1.setVisibility(View.GONE);
                    viewHolder.image2.setVisibility(View.GONE);
                    viewHolder.image.setVisibility(View.VISIBLE);
                    Glide.with(context).load(Constant.IMAGE_PREFIX + images[0]).into(viewHolder.image);
                } else {
                    viewHolder.image1.setVisibility(View.VISIBLE);
                    viewHolder.image2.setVisibility(View.VISIBLE);
                    Glide.with(context).load(Constant.IMAGE_PREFIX + images[0]).into(viewHolder.image1);
                    Glide.with(context).load(Constant.IMAGE_PREFIX + images[1]).into(viewHolder.image2);
                    viewHolder.image.setVisibility(View.GONE);
                }
            }
            viewHolder.play.setVisibility(article.isVideo() ? View.VISIBLE : View.GONE);
            RequestOptions options = new RequestOptions();
            options.placeholder(R.mipmap.default_head_women);
            if(article.getUser()!=null){
                viewHolder.name.setText(article.getUser().getNickname());
                viewHolder.vip.setText("V" + article.getUser().getLevel());
                Glide.with(context).load(Constant.IMAGE_PREFIX + article.getUser().getImgurl()).apply(options).into(viewHolder.head);

            }else {
                viewHolder.name.setText("");
                viewHolder.vip.setText("V" );
                viewHolder.head.setImageResource(R.mipmap.default_head_women);
            }
            if(article.getUser()!=null&&article.getTag()!=null){
                viewHolder.label.setText(article.getTag());
                
            }else {
                viewHolder.label.setText("");
            }
            MoonUtil.identifyFaceExpression(NimUIKit.getContext(), viewHolder.content
                    , article.getContent()
                            .replace(Constant.ARTICLE_P1,"")
                            .replace(Constant.ARTICLE_P2,"")
                            .replace(Constant.ARTICLE_TAG, "")
                    , ImageSpan.ALIGN_BOTTOM);
            viewHolder.comments.setText(String.valueOf(article.getComment()));
            viewHolder.views.setText(String.valueOf(article.getView()) + "人已看");
            viewHolder.praises.setText(String.valueOf(article.getLike()));
            viewHolder.head.setTag(R.id.position, position);

        }else {
            viewHolder.content_lay.setVisibility(View.INVISIBLE);
        }

        return itemView;
    }
    class ViewHolder{
        TextView name, vip, date, content, label, views, praises, comments;
        LinearLayout imageLayout;
        ImageView image1, image2, image, play;
        RoundedImageView head;
        LinearLayout content_lay;

    }
}
